package com.example.lte.util;

import com.example.lte.po.ReqDevice;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lzh
 * @date 2020/11/5 - 19:21
 */
public class DeviceUtil {

    public static ReqDevice getReqDevice(HttpServletRequest request) {
        //用户的发起请求的设备
        //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36
        String userAgent = request.getHeader("user-agent");
        System.out.println(userAgent);
        //解析agent字符串
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        //获取浏览器对象
        Browser browser = ua.getBrowser();
        //获取操作系统对象
        OperatingSystem os = ua.getOperatingSystem();

        return ReqDevice.builder()
                .browserEngine(browser.getRenderingEngine().getName())
                .browserName(browser.getName())
                .browserVersion(browser.getVersion(userAgent).getVersion())
                .deviceType(os.getDeviceType().getName())
                .systemName(os.getName())
                .build();

    }
}
